/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bus.service;

import java.io.File;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.thinkgem.jeesite.common.utils.EnumProperty;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.EnumProperty;
import com.thinkgem.jeesite.common.utils.EnumProperty.AuditTypeEnum;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.bus.dao.EntDao;
import com.thinkgem.jeesite.modules.bus.entity.Address;
import com.thinkgem.jeesite.modules.bus.entity.Audit;
import com.thinkgem.jeesite.modules.bus.entity.Ent;
import com.thinkgem.jeesite.modules.bus.entity.EntDic;
import com.thinkgem.jeesite.modules.sys.entity.Attachment;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.AttachmentService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 养殖场企业信息Service
 *
 * @author mengying
 * @version 2019-05-29
 */
@Service
@Transactional(readOnly = true)
public class EntService extends CrudService<EntDao, Ent> {
    @Autowired
    private EntDao entDao;
    @Autowired
    private AttachmentService attachmentService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private EntDicService entDicService;
    @Autowired
    private AuditService auditService;


    public Ent get(String id) {
        return super.get(id);
    }

    public List<Ent> findList(Ent ent) {
        return super.findList(ent);
    }

    public Page<Ent> findPage(Page<Ent> page, Ent ent) {
        return super.findPage(page, ent);
    }

    @Transactional(readOnly = false)
    public void save(Ent ent) {
        super.save(ent);
    }

    @Transactional(readOnly = false)
    public void delete(Ent ent) {
        super.delete(ent);
    }


    public Map<String, Object> find(String id) throws Exception {
        return dao.find(id);
    }

    @Transactional(readOnly = false)
    public void update(Ent ent) {


        this.entDao.update(ent);
    }

    /**
     * @author Zhang Xingliang
     * @description 养殖场信息备案
     * @date 12:02 2019/6/24
     * @params [ent]
     * @return void
     **/
    @Transactional(readOnly = false)
    public void edit(Ent ent) {
        if (null == ent || null == ent.getAddress()) {
            return;
        }
        boolean isNewRecord = !("0".equals(ent.getAuditStatus()) && !"-1".equals(ent.getAuditId()));
        String auditId = isNewRecord ? IdGen.nextId() : ent.getAuditId();
        this.auditService.save(new Audit(auditId, ent.getId(), "bus_ent", ent.getId(), "", isNewRecord));
        if (null != ent.getAddress().getCounty()) {
            ent.setAreaCode(ent.getAddress().getCounty());
        }
        this.addressService.save(ent.getAddress(), ent.getId(), "bus_ent");

        this.entDicService.save(ent.getEntDicList(), ent.getId(), "bus_ent");

        this.attachmentService.save(ent.getAttachmentList(), ent.getId(), "bus_ent", false);

        ent.setAuditStatus(EnumProperty.AuditTypeEnum.UNREVIEWED.getKey());
        this.entDao.update(ent);
    }

    /***
     * @Author zhangxingliang
     * @Description 获取养殖场养殖品种
     * @Date 11:18 2019/6/3
     * @Param [id]
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     **/
    public List<Map<String, Object>> findEntAnimalList(String id) {
        return dao.findEntAnimalList(id);
    }


    /**
     * @return java.lang.String
     * @Author zhangxingliang
     * @Description 图片保存到服务器指定位置
     * @Date 10:49 2019/6/4
     * @Param [attachment, request]
     **/
    public String uploadImg(Attachment attachment, HttpServletRequest request) throws Exception {
        String newFileName = null;
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        if (multipartResolver.isMultipart(request)) {
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            Iterator<String> iter = multiRequest.getFileNames();
            while (iter.hasNext()) {
                MultipartFile file = multiRequest.getFile(iter.next().toString());
                if (file != null) {
                    String newAppPath = attachment.getFilePath().replace("/", "\\");
                    newFileName = newAppPath.substring(newAppPath.lastIndexOf("\\"), newAppPath.length());
                    newAppPath = newAppPath.substring(0, newAppPath.lastIndexOf("\\"));
                    String path = Global.getConfig("filePath") + File.separator + newAppPath;
                    this.dealSaveImg(file, newFileName, path);
                }
            }
        }
        return newFileName;
    }


    /**
     * @return java.lang.String
     * @Author zhangxingliang
     * @Description 处理图片保存到本地服务器上
     * @Date 10:49 2019/6/4
     * @Param [file, fileName, rootPath]
     **/
    public String dealSaveImg(MultipartFile file, String fileName, String rootPath) {
        File targetFile = new File(rootPath, fileName);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }


	public Ent chenckEntInfo(String entId){
		return entDao.chenckEntInfo(entId);
	}

	/***
	 * @Author mengying
	 * @Description 改变企业审核状态
	 * @Date 10:11 2019/6/3
	 * @Param entId
	 * @return Ent
	 **/
	public boolean updateEntStatus(String entId,String auditStatus){

	    return entDao.updateEntStatus(entId,"",auditStatus);
	};

    /***
     * @Author mengying
     * @Description 养殖场信息查看
     * @Date 10:11 2019/6/22
     * @Param ent
     * @return Page
     **/
    public Page<Ent> viewFarmInfo(Page<Ent> page,Ent ent){
        User user= UserUtils.getUser();
        String areaCode=user.getOffice().getCode();
        ent.setAreaCode(areaCode);
        ent.setPage(page);
        List<Ent> list=entDao.viewFarmInfo(ent);
        page.setList(list);
        return page;

    }

    /**
     * @author Zhang Xingliang
     * @description 养殖场法人身份证唯一性校验
     * @date 14:21 2019/6/26
     * @params [idCard]
     * @return java.lang.String
     **/
    public String checkIdCardExist(String idCard,String id) {
        return dao.checkIdCardExist(idCard,id);
    }

}